using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using OpenQA.Selenium;
using System;
using System.IO;
using System.Threading.Tasks;
using 畜牧业审核程序国产.Models;
using 畜牧业审核程序国产.ViewModels;
using 畜牧业审核程序国产.Views;
using NLog.Config;
using NLog.Targets;
using System.Diagnostics;
using NLog;
using 畜牧业审核程序国产.Models.ToolsClass;

namespace 畜牧业审核程序国产;

public class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        try
        {
            // 初始化 Serilog 记录器
            // InitializeNlog();
            //InitializeLogger();
           

            //设置日志目录环境变量
            StaticLogger.SetLogDirectory(MyDataFolderOperator.LogFolderInAuditProgramFolder);
            // 加载 NLog 配置文件
           // LogManager.LoadConfiguration("NLog.config");
            LogManager.Setup().LoadConfigurationFromFile("Nlog.config");
            Debug.Print($"Log directory: {MyDataFolderOperator.LogFolderInAuditProgramFolder}");
            //程序启动
            StaticLogger.SystemInfo("---------------程序启动V1.1.0----------------");
            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                // Line below is needed to remove Avalonia data validation.
                // Without this line you will get duplicate validations from both Avalonia and CT
                BindingPlugins.DataValidators.RemoveAt(0);
                desktop.MainWindow = new MainWindow
                {
                    DataContext = new MainWindowViewModel()
                };

            }

            base.OnFrameworkInitializationCompleted();
        }
        finally
        {
            // 确保 Serilog 在应用程序结束时正确关闭
            //Log.CloseAndFlush();
        }
    }
    //private void InitializeLogger()
    //{
    //    string logPath = MyDataFolderOperator.LogFolderInAuditProgramFolder;
    //    MyDataFolderOperator.CreateOrCheckAuditProgramFolderExist();
    //    // 配置 Serilog 记录器
    //    Log.Logger = new LoggerConfiguration()
    //           .MinimumLevel.Information()
    //           .WriteTo.File(Path.Combine(logPath, "log.txt"), rollingInterval: RollingInterval.Day)
    //           .WriteTo.Console()
    //           .CreateLogger();

    //    // 记录应用程序启动信息
    //    Log.Information("程序启动");
    //}
    //private static void InitializeNlog()
    //{
    //    LogManager.Configuration = new XmlLoggingConfiguration("NLog.config");
    //    // 获取 NLog 配置
    //    var config = LogManager.Configuration;

    //    // 获取日志文件目标
    //    var logfileTarget = config.FindTargetByName("logfile") as FileTarget;

    //    // 检查目标是否找到
    //    if (logfileTarget == null)
    //    {
    //        Debug.Print("未找到名称为 'logfile' 的目标。请检查 NLog.config 文件。");
    //        return;
    //    }

    //    // 设置日志文件路径（相对路径）
    //    logfileTarget.FileName = Path.Combine(MyDataFolderOperator.AuditProgramFolderPath, "日志.log");
    //    // 调试输出：检查文件路径
    //    Debug.Print($"日志文件路径: {logfileTarget.FileName}");

    //    // 应用配置更改
    //    LogManager.Configuration = config;
    //    LogManager.ReconfigExistingLoggers();

    //    // 创建日志记录器并写入日志
    //    var logger = LogManager.GetCurrentClassLogger();
    //    logger.Info("日志记录已启动");

    //}
}